library(tidyverse)

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_3/")

Dat <- read.csv("RawData.csv", stringsAsFactors = FALSE)


###  Determine respondent threshold for counterforce approval 

Dat <-
  Dat %>%
  mutate(
    NConfirm0 = if_else(is.na(NConfirm0),NA_real_, 0),
    NConfirm01 = if_else(is.na(NConfirm01),NA_real_, 1),
    NConfirm10 = if_else(is.na(NConfirm10),NA_real_, 10),
    NConfirm20 = if_else(is.na(NConfirm20),NA_real_, 20),
    NConfirm30 = if_else(is.na(NConfirm30),NA_real_, 30),
    NConfirm40 = if_else(is.na(NConfirm40),NA_real_, 40),
    NConfirm50 = if_else(is.na(NConfirm50),NA_real_, 50),
    NConfirm60 = if_else(is.na(NConfirm60),NA_real_, 60),
    NConfirm70 = if_else(is.na(NConfirm70),NA_real_, 70),
    NConfirm80 = if_else(is.na(NConfirm80),NA_real_, 80),
    NConfirm90 = if_else(is.na(NConfirm90),NA_real_, 90),
    NConfirm99 = if_else(is.na(NConfirm99),NA_real_, 99),
    NConfirm100 = if_else(is.na(NConfirm100),NA_real_, 100),
    NConfirmNever = if_else(is.na(NConfirmNever),NA_real_, 110),
    outcome = coalesce(
      NConfirm0, NConfirm01, NConfirm10, NConfirm20, NConfirm30, NConfirm40, NConfirm50, NConfirm60, 
      NConfirm70, NConfirm80, NConfirm90, NConfirm99,NConfirm100, NConfirmNever),
    outcome_custom = ifelse(is.na(NConfirmCustom_1),outcome,NConfirmCustom_1),
  )


# Drop NAs (Respondents who did not get to the threshold question)

Dat <-
  Dat %>% 
  filter(!is.na(outcome_custom))

# Sort respondents into Quadrants

Dat <-
  Dat %>% 
  mutate(
    Quadrant = case_when(
      RiskAvg <= mean(RiskAvg) & UtilAvg <= mean(UtilAvg) ~ 1,
      RiskAvg > mean(RiskAvg) & UtilAvg <= mean(UtilAvg) ~ 2,
      RiskAvg <= mean(RiskAvg) & UtilAvg > mean(UtilAvg) ~ 3,
      RiskAvg > mean(RiskAvg) & UtilAvg > mean(UtilAvg) ~ 4,
    )
  )


# Create additional variables
Dat <-
  Dat %>%
  mutate(
    Nuclear = as.numeric(outcome_custom != 110),
    PartyID = case_when(
      Party==1 ~ 0,
      Party==2 ~ 1,
      Party==3 & PartyCloser==2 ~ 0,
      Party==3 & PartyCloser==1 ~ 1,
      Party==4 & PartyCloser==2 ~ 0,
      Party==4 & PartyCloser==1 ~ 1
    )
  )

# Factors for Nuclear Theory

Dat <-
  Dat %>%
  mutate(NuclearTheory_fac = factor(
    NuclearTheory,
    levels = 1:4,
    labels = c(
      "Never-Evers",
      "Existential\nDeterrence",
      "Offense/\nDefense",
      "Utilitarian"
    )
  )) %>% 
  filter(!is.na(NuclearTheory_fac))


# Factors for Quadrants

Dat <-
  Dat %>%
  mutate(
    Quadrant_fac = factor(
      Quadrant,
      levels = 1:4,
      labels = c(
        "Low Harm, Low Risk",
        "Low Harm, High Risk",
        "High Harm, Low Risk",
        "High Harm, High Risk"
      )
    )) %>% 
  filter(!is.na(Quadrant_fac))

CLEANdat <- # Selects columns for export
  Dat %>% 
  select(
    ResponseId,
    UtilAvg,
    RiskAvg,
    NuclearTheory,
    Nuclear,
    NuclearTheory_fac,
    outcome,
    outcome_custom,
    AntiNuclearAll,
    AntiNuclearMost,
    ProNuclearAll,
    ProNuclearMost,
    FreeResponse,
    HeartAttacks,
    Age,
    Party,
    PartyCloser,
    PartyID,
    Gender,
    Ideology,
    Income,
    Military,
    Quadrant,
    Quadrant_fac
  )

write.csv(CLEANdat,"Paper_3_Data_Clean.csv", row.names = FALSE)
